//
//  CutHelper.h
//  Box2DProject
//
//  Created by Thi Huynh on 9/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "cocos2d.h"
#import "Box2D.h"
#import "RaycastCallback.h"
#import "PolygonObject.h"
#import "AbstractLayer.h"
#import "BodyInAABBCallback.h"


#define calculate_determinant_2x2(x1,y1,x2,y2) x1*y2-y1*x2
#define calculate_determinant_2x3(x1,y1,x2,y2,x3,y3) x1*y2+x2*y3+x3*y1-y1*x2-y2*x3-y3*x1

@interface CutHelper : NSObject

- (void)splitBodyWith:(b2World*)world layer:(AbstractLayer*)layer  sprite:(PolygonObject*)sprite;// from:(CGPoint)start to:(CGPoint)end; 

- (void)splitAllBodyWith:(b2World*)world layer:(AbstractLayer*)layer from:(CGPoint)start to:(CGPoint)end;

- (void)splitBodyWith:(b2World*)world layer:(AbstractLayer*)layer atPoint:(CGPoint)point cuts:(int)cuts;

- (std::vector<CGPoint>)getArrayOfCuts:(CGPoint)center radius:(float)radius cuts:(int)cuts;

+ (CutHelper*)getInstance;
@end


class AllBodiesRayCastCallback : public b2RayCastCallback
{
public:
    AllBodiesRayCastCallback()
    {    
    }
    
    float32 ReportFixture(b2Fixture* fixture, const b2Vec2& point,
                          const b2Vec2& normal, float32 fraction){
        
        id userData = (id)fixture->GetBody()->GetUserData();
        if ([userData isKindOfClass:[PolygonObject class]]) 
        {
            rayCastInfo[fixture->GetBody()] = point;
        }
        
        return 1;//go to all other points
    }
    std::map<b2Body*, b2Vec2> rayCastInfo;
};
